home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktimeintro / play movie / start code / playmovie.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-06  |  9.0 KB  |  332 lines

  1. /*
  2.     File:        PlayeMovie.c
  3.     
  4.     Contains:    QuickTime sample code
  5.  
  6.     Copyright:    © 2000 by Apple Computer, Inc. All rights reserved
  7.  
  8.  
  9. */
  10.  
  11.  
  12. /************************************************************
  13. *                                                           *
  14. *    INCLUDE FILES                                          *
  15. *                                                           *
  16. *************************************************************/
  17.  
  18.  
  19. #if !defined(_MSC_VER) && _WIN32
  20.     #include <Win32Headers.mch>
  21.     #define TARGET_OS_WIN32            1
  22. #else
  23.     #include <ConditionalMacros.h>
  24. #endif
  25.  
  26. #if TARGET_OS_WIN32
  27.     #include <QTML.h>
  28.     #define    STRICT
  29.     #include <windows.h>
  30. #endif
  31.  
  32.  
  33. #include "MacTypes.h"
  34.  
  35. #include "MacMemory.h"
  36. #include "Errors.h"
  37. #include "Fonts.h"
  38. #include "QuickDraw.h"
  39. #include "Resources.h"
  40. #include "Gestalt.h"
  41. #include "FixMath.h"
  42. #include "Sound.h"
  43. #include "string.h"
  44. #include "Movies.h"
  45. #include "ImageCompression.h"
  46. #include "Script.h"
  47. #include "TextUtils.h"
  48. #include "Processes.h"
  49.  
  50. #include "PlayMovie.h"
  51. #include "PMUtilities.h"
  52. #include "PMWindow.h"
  53. #include "QTUtilities.h"
  54. #include "ComFramework.h"
  55.  
  56. /************************************************************
  57. *                                                           *
  58. *    FUNCTION PROTOTYPES                                    *
  59. *                                                           *
  60. *************************************************************/
  61.  
  62. #if TARGET_OS_MAC
  63.     static void Utils_Macintosh_DisplayMsg(char *msg);
  64.     static void InitMacToolbox (void);
  65. #else if TARGET_OS_WIN32
  66.     static void Utils_Win32_DisplayMsg(char *msg);
  67. #endif
  68.  
  69.  
  70.  
  71. /************************************************************
  72. *                                                           *
  73. *    CONSTANTS                                              *
  74. *                                                           *
  75. *************************************************************/
  76.  
  77. #define kMsgDialogRsrcID    129
  78. #define kMsgItemID            3
  79.  
  80. #define kPrompt                "Enter the movie file name:"
  81. #define kFileName            "MovieFile.mov"
  82.  
  83. /*
  84. Sample Player's creator type since it is the movie player 
  85. of choice. You can use your own creator type, of course.
  86. */
  87. #define kMyCreatorType        FOUR_CHAR_CODE('TVOD')
  88.  
  89.  
  90. /************************************************************
  91. *                                                           *
  92. *    FUNCTIONS                                              *
  93. *                                                           *
  94. *************************************************************/
  95.  
  96.  
  97.  
  98.  
  99. /************************************************************
  100. *                                                           *
  101. *    CheckError()                                           *
  102. *                                                           *
  103. *    Displays error message if an error occurred            *
  104. *                                                           *
  105. *************************************************************/
  106.  
  107. void CheckError(OSErr error, char *msg)
  108. {
  109.     if (error == noErr)
  110.     {
  111.         return;
  112.     }
  113.     if (strlen(msg) > 0)
  114.     {
  115.         #if TARGET_OS_MAC
  116.             Utils_Macintosh_DisplayMsg(msg);
  117.         #else if TARGET_OS_WIN32
  118.             Utils_Win32_DisplayMsg(msg);
  119.         #endif
  120.         
  121.  
  122.         ExitToShell();
  123.  
  124.     }
  125. }
  126.  
  127. /************************************************************
  128. *                                                           *
  129. *    OpenMovie()                                            *
  130. *                                                           *
  131. *   Open a movie in a new movie window and return true      *
  132. *   if successful.                                          *
  133. *                                                           *
  134. *************************************************************/
  135.  
  136. Boolean OpenMovie(void)
  137. {
  138.     WindowObject            myWindowObject = NULL;
  139.     Movie                    myMovie = NULL;
  140.     WindowReference            myWindow = NULL;
  141.     FSSpec                    myFSSpec;
  142.     short                    myRefNum = kInvalidFileRefNum;
  143.     short                    myResID = 0;
  144.     OSType                     myTypeList[] = {kQTFileTypeMovie, kQTFileTypeQuickTimeImage};
  145.     short                    myNumTypes = 2;
  146.     GrafPtr                    mySavedPort;
  147.     Rect                    myRect = {0, 0, 0, 0};
  148.     Point                    myPoint;
  149.     QTFrameFileFilterUPP    myFileFilterUPP = NULL;
  150.     OSErr                    myErr = noErr;
  151.  
  152. #if TARGET_OS_MAC
  153.     myNumTypes = 0;
  154. #endif
  155.  
  156.     GetPort(&mySavedPort);
  157.     
  158.     // Prompt the user for a movie file
  159.     myFileFilterUPP = QTFrame_GetFileFilterUPP((ProcPtr)QTFrame_FilterFiles);    
  160.     myErr = QTFrame_GetOneFileWithPreview(myNumTypes, (QTFrameTypeListPtr)myTypeList, &myFSSpec, myFileFilterUPP);    
  161.     if (myFileFilterUPP != NULL)
  162.         DisposeNavObjectFilterUPP(myFileFilterUPP);
  163.     if (myErr != noErr)
  164.         goto bail;
  165.     
  166. // Step 1.
  167. // Insert OpenMovieFile.clp here...
  168.  
  169.     if (myErr != noErr)
  170.         goto bail;
  171.  
  172.     // Open the first movie from the file
  173.     myResID = 0;
  174.  
  175. // Step 2.
  176. // Insert NewMovieFromFile.clp here...
  177.  
  178.     if (myErr != noErr)
  179.         goto bail;
  180.     
  181.     // At this point myMovie should be a valid Movie
  182.     // close the movie file as we no longer need it
  183.  
  184. // Step 3.
  185. // Insert CloseMovieFile.clp here...
  186.     
  187.     // For this lab we close the file in the previous call, set myRefNum
  188.     // to kInvalidFileRefNum so QTShell will not attempt to close the file again later
  189.     myRefNum = kInvalidFileRefNum;
  190.     
  191.     // Set the default progress procedure for the movie
  192.     SetMovieProgressProc(myMovie,                // Movie specifier
  193.                         (MovieProgressUPP)-1,    // UPP to a movie progress functions, -1 indicates default function
  194.                         0);                        // Reference constant
  195.  
  196. // Step 4.
  197. // Create the window for the movie        
  198.  
  199.     myWindow = CreateMovieWindow();
  200.     
  201.     if (myWindow == NULL)
  202.         goto bail;    
  203.     myWindowObject = QTFrame_GetWindowObjectFromWindow(myWindow);
  204.     if (myWindowObject == NULL)
  205.         goto bail;    
  206.     QTFrame_SetWindowTitleFromFSSpec(myWindow, &myFSSpec, true);
  207.  
  208.     // Make sure the movie file uses the window GWorld
  209.  
  210. // Step 5.
  211. // Insert SetMovieGWorld.clp here...
  212.         
  213.     // Store movie info in the window record
  214.     (**myWindowObject).fMovie = myMovie;
  215.     (**myWindowObject).fController = NULL; // this sample does not use a movie controller
  216.     (**myWindowObject).fGraphicsImporter = NULL;
  217.     (**myWindowObject).fFileResID = myResID;
  218.     (**myWindowObject).fFileRefNum = myRefNum;
  219.     (**myWindowObject).fCanResizeWindow = false;
  220.     (**myWindowObject).fIsDirty = false;
  221.     (**myWindowObject).fIsQTVRMovie = QTUtils_IsQTVRMovie(myMovie);
  222.     (**myWindowObject).fInstance = NULL;
  223.     (**myWindowObject).fAppData = NULL;
  224.     (**myWindowObject).fFileFSSpec = myFSSpec;
  225.     
  226. // Step 6.    
  227. // Size the window to fit the movie
  228.  
  229.     SizeWindowToMovie(myWindowObject);
  230.  
  231.     // Set the movie's play hints to allow dynamic resizing
  232.  
  233. // Step 7.
  234. // Insert SetMoviePlayHints.clp here...
  235.  
  236. // Step 8.
  237. // Set the movie's position, if it has a 'WLOC' user data atom
  238.     
  239.     myErr = GetWindowPositionFromFile(myMovie, &myPoint);
  240.  
  241.     // show the window
  242. #if TARGET_OS_MAC
  243.     MoveWindow(myWindow, myPoint.h, myPoint.v, false);
  244.     ShowWindow(myWindow);
  245.     SelectWindow(myWindow);                                // make it front-most, since it's just been created
  246.     InvalWindowRect(myWindow, GetWindowPortBounds(myWindow, &myRect));
  247. #endif
  248. #if TARGET_OS_WIN32
  249.     SetWindowPos(myWindow, 0, myPoint.h, myPoint.v, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
  250.     ShowWindow(myWindow, SW_SHOW);
  251.     UpdateWindow(myWindow);
  252. #endif
  253.  
  254. // Step 9.
  255. // Insert PrePrerollMovie.clp here...
  256.         
  257.     return(true);
  258.     
  259. bail:
  260.     if (myWindow != NULL)
  261. #if TARGET_OS_MAC
  262.         DisposeWindow(myWindow);
  263. #endif
  264. #if TARGET_OS_WIN32
  265.         SendMessage(ghWndMDIClient, WM_MDIDESTROY, (WPARAM)myWindow, 0L);
  266. #endif
  267.         
  268.     if (myMovie != NULL)
  269.         DisposeMovie(myMovie);
  270.         
  271.     if (myRefNum != 0)
  272.         CloseMovieFile(myRefNum);
  273.         
  274.     MacSetPort(mySavedPort);    // restore the port that was active when this function was called
  275.  
  276.     return(false);
  277. }
  278.  
  279.  
  280. /************************************************************
  281. *                                                           *
  282. *    FUNCTION:  Utils_Macintosh_DisplayMsg                  *
  283. *                                                           *
  284. *    PURPOSE:   Displays Macintosh error messages           *
  285. *                                                           *
  286. *************************************************************/
  287.  
  288. #if TARGET_OS_MAC
  289. static void Utils_Macintosh_DisplayMsg(char *msg)
  290. {
  291.     DialogPtr theDlog;
  292.     Handle item = NULL;
  293.     Rect box;
  294.     StringPtr theMsg = QTUtils_ConvertCToPascalString(msg);
  295.  
  296.         theDlog = GetNewDialog(kMsgDialogRsrcID, NULL, (WindowPtr)-1);
  297.         if (theDlog != NULL)
  298.         {
  299.             short itemType;
  300.             
  301.                 GetDialogItem(theDlog, kMsgItemID, &itemType, &item, &box);
  302.                 if (item != NULL)
  303.                 {
  304.                     short itemHit;
  305.                     
  306.                         SetDialogItemText(item, theMsg);
  307.                         ModalDialog(NULL, &itemHit);
  308.                         DisposeDialog(theDlog);
  309.                 }
  310.         }
  311.         
  312.         free(theMsg);
  313. }
  314. #endif
  315.  
  316. /************************************************************
  317. *                                                           *
  318. *    FUNCTION:  Utils_Win32_DisplayMsg                      *
  319. *                                                           *
  320. *    PURPOSE:   Displays error messages for Win95/NT sample *
  321. *               code                                        *
  322. *                                                           *
  323. *************************************************************/
  324.  
  325. #if TARGET_OS_WIN32
  326. static void Utils_Win32_DisplayMsg(char *msg)
  327. {
  328.  
  329.     MessageBox(NULL, msg, "", MB_OK);
  330. }
  331. #endif
  332.